package com.yoyound.spider;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.json.JSONObject;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessImpl implements Processable {

    public void process(Page page) {

        HtmlCleaner htmlCleaner = new HtmlCleaner();
         System.out.println(page.getContent());
        TagNode rootNode = htmlCleaner.clean(page.getContent());
        try {
            String goodName = HtmlUtils.getText(rootNode, "//div[@class='item ellipsis']");
            page.setGoodName(goodName);

            List picUrl = HtmlUtils.getListAttributeByName(rootNode, "//div[@class='spec-items']/ul/li/img","data-url","http://img10.360buyimg.com/n1/");
            page.setPicUrl(picUrl);


            String url = page.getDataUrl();
            Pattern compile = Pattern.compile("http://item.jd.com/([0-9]+).html");
            Matcher matcher = compile.matcher(url);
            String goodid = null;
            if (matcher.find()) {
                goodid = matcher.group(1);
                page.setGoodId(goodid);
            }
            String dispicUrl = HtmlUtils.getAttributeByScript(rootNode, "//script","src","http:");
            String pricejson = PageUtil
                    .getContent("http:"+dispicUrl);
            if(pricejson.contains("showdesc")){
                pricejson=pricejson.replace("showdesc(","").replace(")","");
            }
            System.out.println("pricejson="+pricejson);
            JSONObject j=new JSONObject(pricejson);
            HtmlCleaner disClean = new HtmlCleaner();
            //System.out.println(page.getContent());
            TagNode disNode = disClean.clean(j.getString("content"));

            List disUrl = HtmlUtils.getListAttributeByName(disNode, "//img","data-lazyload","http:");

            if(disUrl.size()==0){
                disUrl =HtmlUtils.getText(disNode, "//style","http:");
            }
            if(disUrl.size()!=0){
                page.setDisUrl(disUrl);
            }
           // System.out.println(disUrl);

            //page.setDisUrl(dispicUrl);
          /*
            String pricejson = PageUtil
                    .getContent("http://p.3.cn/prices/get?skuid=J_" + goodid);
            JSONArray jsonArray = new JSONArray(pricejson);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            String price = jsonObject.getString("p");
            page.setPrice(price);*/



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String pricejson="{\"date\":1558516245479,\"content\":\"<style>.ssd-module-wrap{width:750px;margin:0 auto;position:relative;text-align:left;background-color:#ffffff;}.ssd-module-wrap .ssd-module,.ssd-module-wrap .ssd-module-heading{width:750px;position:relative;overflow:hidden;}.ssd-module{background-repeat:no-repeat;background-position:left top;background-size:100% 100%;}.ssd-module .ssd-widget-pic,.ssd-module .ssd-widget-text,.ssd-module .ssd-widget-line,.ssd-module-wrap .ssd-widget-rectangle,.ssd-module-wrap .ssd-widget-circle,.ssd-module-wrap .ssd-widget-triangle,.ssd-module-wrap .ssd-widget-table{position:absolute;overflow:hidden;}.ssd-module-wrap .ssd-widget-table table{width:100%;height:100%}.ssd-module-wrap .ssd-widget-table td{position:relative;white-space:pre-line;word-break:break-all;}.ssd-module .ssd-widget-pic img{display:block;width:100%;height:100%}.ssd-module .ssd-widget-text{position:absolute;overflow:hidden;}.ssd-module .ssd-widget-text span{display:block;overflow:hidden;width:100%;height:100%;padding:0;margin:0;word-break:break-all;word-wrap:break-word;white-space:normal;}.ssd-module .ssd-widget-link{position:absolute;left:0;top:0;width:100%;height:100%;background:transparent;z-index:100}.ssd-module-wrap .ssd-widget-text{line-height:1.5;word-break:break-all;}.ssd-module-wrap .ssd-cell-text{position:absolute;top:0;left:0;right:0;width:100%;height:100%;overflow:auto;}.ssd-module-heading{background-repeat:no-repeat;background-position:left center;background-size:100% 100%;}.ssd-module-heading .ssd-module-heading-layout{display:inline-block;}.ssd-module-heading .ssd-widget-heading-ch{float:left;display:inline-block;margin-left:15px;margin-right:6px;height:100%;}.ssd-module-heading .ssd-widget-heading-en{float:left;display:inline-block;margin-left:6px;margin-right:15px;height:100%;}.ssd-module-wrap .ssd-widget-rectangle{box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;}.ssd-module-wrap .M15513421557391{width:750px; background-color:#d7d7d7; background-image:url(//img30.360buyimg.com/sku/jfs/t1/32691/19/3746/354674/5c779936Ea9552d19/421b371ae8575d40.jpg); height:675px}\\n.ssd-module-wrap .M15513421557482{width:750px; background-color:#cbcbcb; background-image:url(//img30.360buyimg.com/sku/jfs/t1/20781/3/8555/167025/5c779936E1b8f87b3/d7b3c33df85a7586.jpg); height:675px}\\n.ssd-module-wrap .M15513421557603{width:750px; background-color:#d7d7d7; background-image:url(//img30.360buyimg.com/sku/jfs/t1/17953/33/8614/155684/5c779936E47de8285/44313b6ef893a85d.jpg); height:674px}\\n.ssd-module-wrap .M15513421557714{width:750px; background-color:#cbcbcb; background-image:url(//img30.360buyimg.com/sku/jfs/t1/20482/28/8561/157160/5c779936E6e5048a9/46d3848b98209597.jpg); height:675px}\\n.ssd-module-wrap .M15513421557815{width:750px; background-color:#f2f2f2; background-image:url(//img30.360buyimg.com/sku/jfs/t1/10922/22/12208/170557/5c779936E647ae33d/c1db6b8b739aa514.jpg); height:675px}\\n.ssd-module-wrap .M15513421557966{width:750px; background-color:#d7d7d7; background-image:url(//img30.360buyimg.com/sku/jfs/t1/9219/35/15543/100655/5c779935Ecbb66643/f7cca4ba3618138b.jpg); height:675px}\\n.ssd-module-wrap .M15513421558087{width:750px; background-color:#e9e9e9; background-image:url(//img30.360buyimg.com/sku/jfs/t1/21384/3/8588/113373/5c779935Efd27e55f/cd8bae9a90b2a79b.jpg); height:674px}\\n.ssd-module-wrap .M15513421558238{width:750px; background-color:#cbcbcb; background-image:url(//img30.360buyimg.com/sku/jfs/t1/11579/30/9430/79965/5c779935E80396d9d/7619ed195cc217df.jpg); height:675px}\\n.ssd-module-wrap .M15513421558349{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/30953/3/3763/89656/5c779935E88d8ede9/454f9efb794e02d9.jpg); height:675px}\\n.ssd-module-wrap .M155134215584610{width:750px; background-color:#f2f2f2; background-image:url(//img30.360buyimg.com/sku/jfs/t1/28841/16/8549/102147/5c779935E9bb28433/b3ad9bd9218943e9.jpg); height:675px}\\n.ssd-module-wrap .M155134215586011{width:750px; background-color:#cbcbcb; background-image:url(//img30.360buyimg.com/sku/jfs/t1/31599/23/3777/77814/5c779935E06a7d341/168fae4d43c95c20.jpg); height:674px}\\n.ssd-module-wrap .M155134215587412{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/8176/22/16180/124576/5c779935E1e0ded0e/584c77d9a3ec1d40.jpg); height:675px}\\n.ssd-module-wrap .M155134215589113{width:750px; background-color:#f2f2f2; background-image:url(//img30.360buyimg.com/sku/jfs/t1/22347/32/8565/115058/5c779935Ef9be550f/bdba19216213e549.jpg); height:675px}\\n.ssd-module-wrap .M155134215590714{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/32412/15/3700/97593/5c779935E50fc52ce/0a7d47a19ccb4ad4.jpg); height:675px}\\n.ssd-module-wrap .M155134215592415{width:750px; background-color:#cbcbcb; background-image:url(//img30.360buyimg.com/sku/jfs/t1/9117/10/16268/134521/5c779936Ea61199eb/b80e4aad40c4ff60.jpg); height:674px}\\n.ssd-module-wrap .M155134215594016{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/22949/37/8534/164567/5c779936Eb5b08282/06b101e45f00a2fe.jpg); height:675px}\\n.ssd-module-wrap .M155134215596117{width:750px; background-color:#e9e9e9; background-image:url(//img30.360buyimg.com/sku/jfs/t1/20945/25/8623/125118/5c779936E2e276225/40d3b4f1fee4abf1.jpg); height:675px}\\n.ssd-module-wrap .M155134215598018{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/16067/38/8544/187868/5c779936Ee481829e/41e1a92140384784.jpg); height:675px}\\n.ssd-module-wrap .M155134215600019{width:750px; background-color:#d7d7d7; background-image:url(//img30.360buyimg.com/sku/jfs/t1/26903/23/8719/123646/5c779935E6366baeb/58e4ac1462f44e52.jpg); height:674px}\\n.ssd-module-wrap .M155134215602020{width:750px; background-color:#b3b3b3; background-image:url(//img30.360buyimg.com/sku/jfs/t1/25889/1/8469/99894/5c779935E6b3ec3c9/942dfbfb9ca854b8.jpg); height:675px}\\n</style><br/><div id='zbViewModulesH'  value='13495'></div><input id='zbViewModulesHeight' type='hidden' value='13495'/><div skudesign=\\\"100010\\\"></div><div class=\\\"ssd-module-wrap\\\" >\\n    <div class=\\\"ssd-module M15513421557391 animate-M15513421557391\\\" data-id=\\\"M15513421557391\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421557482 animate-M15513421557482\\\" data-id=\\\"M15513421557482\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421557603 animate-M15513421557603\\\" data-id=\\\"M15513421557603\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421557714 animate-M15513421557714\\\" data-id=\\\"M15513421557714\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421557815 animate-M15513421557815\\\" data-id=\\\"M15513421557815\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421557966 animate-M15513421557966\\\" data-id=\\\"M15513421557966\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421558087 animate-M15513421558087\\\" data-id=\\\"M15513421558087\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421558238 animate-M15513421558238\\\" data-id=\\\"M15513421558238\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M15513421558349 animate-M15513421558349\\\" data-id=\\\"M15513421558349\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215584610 animate-M155134215584610\\\" data-id=\\\"M155134215584610\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215586011 animate-M155134215586011\\\" data-id=\\\"M155134215586011\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215587412 animate-M155134215587412\\\" data-id=\\\"M155134215587412\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215589113 animate-M155134215589113\\\" data-id=\\\"M155134215589113\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215590714 animate-M155134215590714\\\" data-id=\\\"M155134215590714\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215592415 animate-M155134215592415\\\" data-id=\\\"M155134215592415\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215594016 animate-M155134215594016\\\" data-id=\\\"M155134215594016\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215596117 animate-M155134215596117\\\" data-id=\\\"M155134215596117\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215598018 animate-M155134215598018\\\" data-id=\\\"M155134215598018\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215600019 animate-M155134215600019\\\" data-id=\\\"M155134215600019\\\">\\n        \\n</div>\\n<div class=\\\"ssd-module M155134215602020 animate-M155134215602020\\\" data-id=\\\"M155134215602020\\\">\\n        \\n</div>\\n\\n</div>\\n<!-- 2019-02-28 04:23:03 --> <br/><script></script><br/>\"}";
        JSONObject j=new JSONObject(pricejson);
        HtmlCleaner disClean = new HtmlCleaner();
        TagNode disNode = disClean.clean(j.getString("content"));
        List<String> disUrl  = HtmlUtils.getText(disNode, "//style","http:");
        for (String s:disUrl
             ) {
            System.out.println(s);
        }
    }

}
